﻿import json

from django.http import HttpResponseRedirect
from django.http import HttpResponse, HttpResponseBadRequest
from django.contrib.auth.forms import PasswordChangeForm
from django.views.generic.base import View, TemplateResponseMixin
from django.views.generic import FormView
from django.contrib.auth import logout
from django_tables2.config import RequestConfig

from clients.tables import *
from clients.forms import ClientForm


# Create your views here.
class GtLinkBaseView(View, TemplateResponseMixin):
    def get(self, request):
        context = self.get_context(request)
        return TemplateResponseMixin.render_to_response(self, context)


class Index(GtLinkBaseView):
    template_name = 'main/index.html'

    def get_context(self, request):
        if hasattr(request.user, 'gtlinkuser'):
            client = request.user.gtlinkuser.client
            context = dict()
            context['client_name'] = client.name
            context['balance'] = client.balance()
            context['bonuses'] = client.bonus_points()
            context['monthly'] = client.monthly_cost()
            return context


def LogoutView(request):
    logout(request)
    return HttpResponseRedirect("/")


class FinanceView(GtLinkBaseView):
    template_name = 'main/finance.html'

    def get_context(self, request):
        if hasattr(request.user, 'gtlinkuser'):
            client = request.user.gtlinkuser.client
            context = dict()
            operations_query =  client.operations.all().order_by('-date')
            bonuses_query = client.bonuses.all().order_by('-date')
            context['operations'] =OperationsTable(operations_query, prefix='op-',empty_text=u'Операций пока нет')
            context['bonuses'] = BonusesTable(bonuses_query, prefix='bns-', empty_text=u'Бонусов пока нет')
            RequestConfig(request).configure(context['operations'])
            RequestConfig(request).configure(context['bonuses'])
            return context

class AgreementsView(GtLinkBaseView):
    template_name = 'main/agreements.html'

    def get_context(self,request):
        if hasattr(request.user, 'gtlinkuser'):
            client = request.user.gtlinkuser.client
            context = dict()
            agreements_query = client.agreements.all().order_by('number')
            context['agreements'] = AgreementsTable(agreements_query)
            return context

class PersonalView(GtLinkBaseView):
    template_name = 'main/personal.html'

    def get_context(self,request):
        context = dict()
        context['client_form'] = ClientForm(instance=request.user.gtlinkuser.client)
        return context

class ServicesView(GtLinkBaseView):
    template_name = 'main/services.html'

    def get_context(self, request):
        context = dict()
        client = request.user.gtlinkuser.client
        services_query = client.services.all()
        context['services'] = ServicesTable(services_query)
        return context

class PaymentView(GtLinkBaseView):
    template_name = 'main/payment.html'

    def get_context(self, request):
       context = dict()
       client = request.user.gtlinkuser.client
       client_agreements = client.agreements.select_related().all()
       agreements_list = list()
       for agreement in client_agreements:
           agreements_list.append({'number':agreement.number,'balance': agreement.balance(), 'monthly': agreement.monthly_cost()})
       context['client'] = client.name
       context['agreements'] = agreements_list
       return context

class AjaxFormView(FormView):
    def form_valid(self, form):
        form.save()
        return HttpResponse("OK")

class PasswordChange(AjaxFormView):
    template_name = "main/password_change.html"
    form_class = PasswordChangeForm

    def get_form_kwargs(self):
        kwargs = super(PasswordChange, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs


